<?php
///////////////////////////////////////////////////////////////////////////////
/*
leveltype.php

Class Leveltype - class containing methods for retrieving and saving leveltype data.


*/

class Leveltype {

    private $id;
    private $name;
    private $description;

    public static $TABLE_LEVELTYPE = TABLE_LEVELTYPE;

    /////////////////////////////////////////////////////////////////////////////
    // contructor - instantiates leveltype object with supplied attributes
    public function __construct($id = null, $name = null, $description = null) {
        $this->init($id, $name, $description);
    }

    /////////////////////////////////////////////////////////////////////////////
    // init - convenience function to set all leveltype attribute values at once
    public function init($id = null, $name = null, $description = null) {
        $this->setID($id);
        $this->setName($name);
        $this->setDescription($description);
    }

    /////////////////////////////////////////////////////////////////////////////
    // load - initialize the leveltype by loading it from the database based on the
    // specificed id
    public function load($id) {
        $sql = "SELECT id, name, description " .
             "FROM " . Leveltype::$TABLE_LEVELTYPE . " where id = " . $id;
        $result = DatabaseManager::getInstance()->query($sql);

        if ($row = $result->fetch_assoc()) {
            $this->init($row['id'], $row['name'], $row['description']);
        } else {
            return false;
        }
        return true;
    }

    /////////////////////////////////////////////////////////////////////////////
    // insert - insert a new leveltype into the database and return the new Leveltype
    // Don't insert if it already exists.  That is, name already exists.
    public function insert() {
        if ($this->existsInDatabase()) {
            // print("leveltype already exists in database\n");
            return false;
        }
        $sql = "CALL insertLeveltype('{$this->getName()}', " .
                                   "'{$this->getDescription()}', " .
                               "@newLeveltypeID)";

        $result = DatabaseManager::getInstance()->query($sql);
        $sql = "select @newLeveltypeID";
        $result = DatabaseManager::getInstance()->query($sql);
        if ($row = $result->fetch_assoc()) {
            $this->setID($row["@newLeveltypeID"]);
        } else {
            return false;
        }
        return true;
    }

    /////////////////////////////////////////////////////////////////////////////
    // update - update the leveltype to the database (leveltype already exists in database)
    public function update() {

        $sql = "UPDATE " . Leveltype::$TABLE_LEVELTYPE .
                              " set name = '{$this->getName()}', " .
                                   "description = '{$this->getDescription()}' " .
                                   "WHERE id = {$this->getID()}";
        $result = DatabaseManager::getInstance()->query($sql);
    }

    /////////////////////////////////////////////////////////////////////////////
    // delete - delete the leveltype from the database - probably rarely or never
    // want to do this!
    public function delete() {
        $sql = "DELETE FROM " . Leveltype::$TABLE_LEVELTYPE . " WHERE id = {$this->getID()}";
        $result = DatabaseManager::getInstance()->query($sql);
        return true;
    }

    /////////////////////////////////////////////////////////////////////////////
    // existsInDatabase - checks to see if leveltype already exists in database -
    // meaning a leveltype record with name equal to the value in this Leveltype object.
    private function existsInDatabase() {
        $leveltypeCount = 0;
        $sql = "SELECT count(*) as leveltype_count FROM " . Leveltype::$TABLE_LEVELTYPE .
               " where name = '{$this->getName()}' ";
        $result = DatabaseManager::getInstance()->query($sql);
        if ($row = $result->fetch_assoc()) {
            $leveltypeCount = $row['leveltype_count'];
        } else {
            // error handling?
        }
        if ($leveltypeCount < 1) {
            return false;
        }
        return true;
    }


    /////////////////////////////////////////////////////////////////////////////
    // getID - get the node id
    public function getID() {
        return $this->id;
    }

    /////////////////////////////////////////////////////////////////////////////
    // setID - set the node id
    public function setID($id) {
        $this->id = $id;
    }

    /////////////////////////////////////////////////////////////////////////////
    // getName - get the name
    public function getName() {
        return $this->name;
    }

    /////////////////////////////////////////////////////////////////////////////
    // setName - set the name
    public function setName($name) {
        $this->name = $name;
    }

    /////////////////////////////////////////////////////////////////////////////
    // getDescription - get the description
    public function getDescription() {
        return $this->description;
    }
    /////////////////////////////////////////////////////////////////////////////
    // setDescription - set the description
    public function setDescription($description) {
        $this->description = $description;
    }



    /////////////////////////////////////////////////////////////////////////////
    // printLeveltypeData - debug function that dumps leveltype attributes
    public function printLeveltypeData($desc = "") {
        //print("--------------------------------------------\n");
        print("{$desc}:\n");
        print("\tID: {$this->getID()}\n");
        print("\tNAME: {$this->getName()}\n");
        print("\tDESCRIPTION: {$this->getDescription()}\n");
    }





    // Static functions - mainly for retrieving groups of leveltypes

    /////////////////////////////////////////////////////////////////////////////
    public static function loadLeveltypes($condition) {
        $leveltypes = array();

        $sql = "SELECT id, name, description FROM " .
               Leveltype::$TABLE_LEVELTYPE . " where " .
               $condition;               


        $result = DatabaseManager::getInstance()->query($sql);
        while ($row = $result->fetch_assoc()) {
            $leveltype = new Leveltype($row['id'],
                             $row['name'],
                             $row['description']);
            $leveltypes[] = $leveltype;
        }
        return $leveltypes;
    }


    /////////////////////////////////////////////////////////////////////////////
    public static function loadAllLeveltypes() {
        return Leveltype::loadLeveltypes("1");
    }






    /////////////////////////////////////////////////////////////////////////////
    public static function printLeveltypesData($leveltypes, $desc = "") {
        print($desc . "\n");
        $i = 0;
        foreach ($leveltypes as $leveltype) {
            $i++;
            $leveltype->printLeveltypeData("Leveltype " . $i);
        }
        print(count($leveltypes) . " leveltypes\n");
    }




}

